<?php
require_once 'Zend/Db.php';
class synchost{
	private $config;
	function __construct(){
	    $this->config = new Zend_Config(require 'inc/dbcnf.php');
	}
	private function db131()
    {   
		return Zend_Db::factory($this->config->db_131);
    }
	private function db()
    {   
		return Zend_Db::factory($this->config->db_mwall);
    } 
	public function syncSCS(){
		$db131 = $this->db131();
		$db = $this->db();
		$hosttypeid = $db->fetchOne("select id from 	hosttypes where name='scs' limit 1");
		$devrows = $db131->fetchAll("SELECT vdg.vdg_parentid as vdg_pid,b.vdg_name as vdg_pname,vdg.vdg_id,vdg.vdg_name,vsb.vsb_id,vsb.vsb_serialid,vsb.vsb_name,vsbn.vsbn_ip FROM vlog_switchandbroker vsb,vlog_device_group vdg,vlog_device_type_content vdtc,vlog_device_group b,vlog_operators vo, vlog_switchandbroker_netcard  vsbn WHERE b.vdg_id=vdg.vdg_parentid and vdg.vdg_operatorid=vo.vo_id and vo.vo_deletetype=0 and vo.vo_parentid=18 and vsb.vsb_id=vsbn.vsbn_vsbid and vdg.vdg_id=vsb.vsb_groupid  and vdg.vdg_deletetype=0 and vsb.vsb_vdtcid=vdtc.vdtc_id and vsb.vsb_deletetype=0 and vsb.vsb_vdgo_id=1 and vsb.vsb_type=0 and vsb.vsb_svc_usetype<>99 group by vsb.vsb_id order by vdg.vdg_parentid,vsb.vsb_groupid");
		foreach ($devrows as $row){
			$hostip = $row["vsbn_ip"] ;
			$area1 = $row["vdg_pname"];
			$area2 = $row["vdg_name"];
			$devid = $row["vsb_serialid"];
			$hostdes = $row["vsb_name"];
			$num = $db->fetchOne("select count(*) from hosts where devid = '$devid' ");
		    if($num =="0"){	$db->query("insert into hosts(hosttypeid, hostip, area1, area2, devid, hostdes) values(:hosttypeid, :hostip, :area1, :area2, :devid, :hostdes)",array("hosttypeid"=>$hosttypeid, "hostip"=>$hostip, "area1"=>$area1, "area2"=>$area2, "devid"=>$devid, "hostdes"=>$hostdes));
			}
			else{
				$db->query("update hosts set hosttypeid=:hosttypeid, hostip=:hostip, area1=:area1, area2=:area2, hostdes=:hostdes where devid=:devid",array("hosttypeid"=>$hosttypeid, "hostip"=>$hostip, "area1"=>$area1, "area2"=>$area2 , "hostdes"=>$hostdes  , "devid"=>$devid));
			}
		}
	}
	
	public function syncSAS(){
       	$db131 = $this->db131();
		$db = $this->db();
		$hosttypeid = $db->fetchOne("select id from 	hosttypes where name='sas' limit 1");
       	$devrows = $db131->fetchAll("SELECT vdg.vdg_parentid as vdg_pid,b.vdg_name as vdg_pname,vdg.vdg_id,vdg.vdg_name,vsb.vsb_id,vsb.vsb_serialid,vsb.vsb_name,vsbn.vsbn_ip FROM vlog_switchandbroker vsb,vlog_device_group vdg,vlog_device_type_content vdtc,vlog_device_group b,vlog_operators vo, vlog_switchandbroker_netcard  vsbn WHERE b.vdg_id=vdg.vdg_parentid and vdg.vdg_operatorid=vo.vo_id and vo.vo_deletetype=0 and vo.vo_parentid=18 and vsb.vsb_id=vsbn.vsbn_vsbid and vdg.vdg_id=vsb.vsb_groupid  and vdg.vdg_deletetype=0 and vsb.vsb_vdtcid=vdtc.vdtc_id and vsb.vsb_deletetype=0 and vsb.vsb_vdgo_id=1 and vsb.vsb_type=1 and vsb.vsb_svc_usetype<>99 group by vsb.vsb_id order by vdg.vdg_parentid,vsb.vsb_groupid");
		foreach ($devrows as $row){
            $hostip = $row["vsbn_ip"] ;
            $area1 = $row["vdg_pname"];
            $area2 = $row["vdg_name"];
            $devid = $row["vsb_serialid"];
            $hostdes = $row["vsb_name"];
            $num = $db->fetchOne("select count(*) from hosts where devid = '$devid' ");
            if($num =="0"){ $db->query("insert into hosts(hosttypeid, hostip, area1, area2, devid, hostdes) values(:hosttypeid, :hostip, :area1, :area2, :devid, :hostdes)",array("hosttypeid"=>$hosttypeid, "hostip"=>$hostip, "area1"=>$area1, "area2"=>$area2, "devid"=>$devid, "hostdes"=>$hostdes));
            }   
			else{
                $db->query("update hosts set hosttypeid=:hosttypeid, hostip=:hostip, area1=:area1, area2=:area2, hostdes=:hostdes where devid=:devid",array("hosttypeid"=>$hosttypeid, "hostip"=>$hostip, "area1"=>$area1, "area2"=>$area2 , "hostdes"=>$hostdes  , "devid"=>$devid));
            }   
        }   
    }  
	public function syncSBS(){
		$db131 = $this->db131();
		$db = $this->db();
		$hosttypeid = $db->fetchOne("select id from 	hosttypes where name='sbs' limit 1");
		$devrows = $db131->fetchAll("select VB_NAME, VB_SERIALID,VB_CONTROLIP from smpvsdn.vlog_balance where VB_DELETETYPE = 0 and VB_SVC_USETYPE <>99");
		foreach ($devrows as $row){
            $hostip = $row["VB_CONTROLIP"] ;
            $devid = $row["VB_SERIALID"];
            $hostdes = $row["VB_NAME"];
            $num = $db->fetchOne("select count(*) from hosts where devid = '$devid' ");
            if($num =="0"){ $db->query("insert into hosts(hosttypeid, hostip, devid, hostdes) values(:hosttypeid, :hostip, :devid, :hostdes)",array("hosttypeid"=>$hosttypeid, "hostip"=>$hostip, "devid"=>$devid, "hostdes"=>$hostdes));
            }
			else{
				$db->query("update hosts set hosttypeid=:hosttypeid, hostip=:hostip,hostdes=:hostdes where devid=:devid",array("hosttypeid"=>$hosttypeid, "hostip"=>$hostip,"hostdes"=>$hostdes, "devid"=>$devid));
			}
        }   
	}
	
}
$qos = new synchost();
$qos->syncSCS();
$qos->syncSAS();
$qos->syncSBS();
#$result = $qos->GetTxt("http://192.168.1.236:811/test/demo.html");
#include("MSLine.php");
